import {
  getToken,
  setToken,
  removeToken,
  getUserName,
  setUserName,
  removeUserName,
  getUserId,
  setUserId,
  removeUserId,
  getUserAvatar,
  setUserAvatar,
  removeUserAvatar,
} from '@/utils/auth';
import {login, logout, getInfo} from '@/api/login';

const user = {
  state: {
    token: getToken() || '',
    id: getUserId() || '',
    name: getUserName() || '',
    avatar: getUserAvatar() || '',
    
    need_login: true, //是否显示登录提示，默认需要显示
  },
  
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
      setToken(token);
    },
    SET_ID: (state, id) => {
      state.id = id;
      setUserId(id);
    },
    SET_NAME: (state, name) => {
      state.name = name;
      setUserName(name);
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
      setUserAvatar(avatar);
    },
    SET_NEED_LOGIN: (state, need) => {
      state.need_login = need;
    },
  },
  
  actions: {
    // 登录
    Login({commit}, loginInfo)
    {
      return new Promise((resolve, reject) => {
        login(loginInfo).then(response => {
          const data = response.data;
          commit('SET_TOKEN', data.token);
          commit('SET_ID', data.id);
          commit('SET_NAME', data.userName);
          commit('SET_AVATAR',data.shopLogo || '');
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },
    
    // 获取用户信息
    GetInfo({commit, state})
    {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response.data;
          resolve(data);
        }).catch(error => {
          reject(error);
        });
      });
    },
    
    // 登出
    LogOut({commit, state})
    {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '');
          commit('SET_NAME', '');
          commit('SET_ID', '');
          //commit('SET_AVATAR', '');
          commit('SET_NEED_LOGIN', true);
          
          removeToken();
          removeUserName();
          removeUserId();
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },
    
    // 前端 登出
    FedLogOut({commit})
    {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        commit('SET_NAME', '');
        commit('SET_ID', '');
        //commit('SET_AVATAR', '');
        commit('SET_NEED_LOGIN', true);
        
        removeToken();
        removeUserName();
        removeUserId();
        resolve();
      });
    },
    Needlogin({commit, state}, need)
    {
      return new Promise((resolve, reject) => {
        if(need && state.need_login) //需要登录提示，resolve，否则reject
        {
          //设置不需要重复提示
          commit('SET_NEED_LOGIN', false);
          resolve();
          
          //重置
          setTimeout(()=>{
            commit('SET_NEED_LOGIN', true);
          },2000);
        }
        else
        {
          reject();
        }
        
      });
      
    },
    updateUserAvatar({commit}, avatar)
    {
      return new Promise(resolve => {
        commit('SET_AVATAR', avatar);
        resolve();
      });
    }
    
  },
  
  getters: {
    token: state => state.token,
    userName: state => state.name,
    userId: state => state.id,
    userAvatar: state => state.avatar,
  }
};

export default user;
